#include<stdio.h>
#include<windows.h>
#include<conio.h> //定义全局变量
#include<windows.h>
#include<time.h>


#define high 20
#define width 50 //游戏画面尺寸
#define enemynum 3


int canvas[high][width]={{0}};//二维数组记录游戏画面中对应的元素
                        //0输出空格，1输出飞机2为子弹3为敌机
int position_x,position_y;
int enemy_x[enemynum],enemy_y[enemynum];
int score;
int bulletwidth;
int enemymovespeed;


void HideCursor()			//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)		//光标移动到(x,y)位置,清屏函数
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void startup()		//数据初始化
{
    position_x=high/2;
    position_y=width/2;
    canvas[position_x][position_y]=1;

    int k;
    for(k=0;k<enemynum;k++)
    {
        enemy_x[k]=rand()%high;
        enemy_y[k]=rand()%width;
        canvas[enemy_x[k]][enemy_y[k]]=3;
    }
    bulletwidth=2;
    score=0;
    enemymovespeed=20;
    HideCursor();    //隐藏光标

}
void show()				//显示界面
{
    gotoxy(0,0);
    int i,j;
    for(i=0;i<=high-1;i++)
    {
        for (j=0;j<width-1;j++)
        {

            if(canvas[i][j]==1)
                printf("*");//输出飞机
            else if(canvas[i][j]==2)
                printf("|");//输出子弹
            else if(canvas[i][j]==3)
                printf("@");//输出敌机
            else
                printf(" ");//输出空格
        }
        printf("\n");
    }
    printf("得分：%d\n",score);
}
void updateWithoutInpute()
//与用户输入无关的更新
{
    int i,j,k;

    for(i=0;i<=high-1;i++)
    {
        for (j=0;j<width-1;j++)
        {
            if(canvas[i][j]==2)
            {
                for(k=0;k<enemynum;k++)
                {
                     if(i==enemy_x[k]&&j==enemy_y[k])  //击中
                    {
					    score++;
                        if(score%5==0 && enemymovespeed>15)   //避免速度过快
                            enemymovespeed--;

                        if(score%5==0)  //一定分数后子弹加宽
                            bulletwidth++;

                        enemy_x[k]=rand()%2;
                        enemy_y[k]=rand()%width;

                        canvas[enemy_x[k]][enemy_y[k]]=3;
                        canvas[i][j]=0;
                     
                    }
                }
                canvas[i][j]=0;   //子弹消失
                if(i>0)
                    canvas[i-1][j]=2;  //子弹上移
            }

        }
    }


    static int speed=10;
    if(speed<enemymovespeed)
        speed++;
    for(k=0;k<enemynum;k++)
    {
        if((position_x==enemy_x[k])&&(position_y==enemy_y[k]))
        {
            printf("失败！\n");
                Sleep(3000);
                system("pause");
                exit(0);
        }


        if(enemy_x[k]>high)
        {
            canvas[enemy_x[k]][enemy_y[k]]=0;
            enemy_x[k]=rand()%2;
            enemy_y[k]=rand()%width;
            canvas[enemy_x[k]][enemy_y[k]]=3;
            score--;
        }

		
		if(speed==enemymovespeed)
		{
				//敌机自动下落
			for(k=0;k<enemynum;k++)
			{
				canvas[enemy_x[k]][enemy_y[k]]=0;
					enemy_x[k]++;
					speed=0;
				canvas[enemy_x[k]][enemy_y[k]]=3;
			}

		}
	}
}
void updateWithInpute()
//与用户输入有关的更新
{
    char input;
    if(kbhit())
    {

        input=getch();
        if(input=='a')
        {canvas[position_x][position_y]=0;
            position_y--;
             canvas[position_x][position_y]=1;
        }
        else if(input=='d')
        {
            canvas[position_x][position_y]=0;
            position_y++;
            canvas[position_x][position_y]=1;
        }
       else if(input=='s')
        {
            canvas[position_x][position_y]=0;
            position_x++;
            canvas[position_x][position_y]=1;
        }
        else if(input=='w')
        {
            canvas[position_x][position_y]=0;
            position_x--;
            canvas[position_x][position_y]=1;
        }
        else if(input==' ')
        {
            int left,right;
            left=position_y-bulletwidth/2;
            if(left<0)
                left=0;

            right=position_y+bulletwidth-bulletwidth/2;
            if(right>width-1)
                right=width-1;
            int y;
            for(y=left;y<=right;y++)
            {
                canvas[position_x-1][y]=2;
            }
        }
    }


}
int main()
{
	startup();
	while (1)
	{
		show();
		//显示界面
		updateWithoutInpute();
		//与用户输入无关的更新
		updateWithInpute();	        //与用户输入有关的更新
	}
	return 0;
}